<?php
class cdb {
	var $version = '';
	var $charset = '';
	var $dbhost = '';
	var $dbuser = '';
	var $dbpw = '';
	var $dbname = '';
	var $connect;

	function commit() {
		$this->_commit();
	}
	
	/**
	 * 插入数据. 成功的话,返回插入的id(>0), 否则返回-1
	 * @param $table
	 * @param $values
	 */
	function insert($table, $values) {
		return -1;
	}

	/**
	 * 更新操作. 成功返回更新的行数, 否则返回-1
	 * 
	 * @param unknown_type $table
	 * @param unknown_type $values
	 * @param unknown_type $where
	 * @param unknown_type $updateall 当$where为空时,是否要更新全表,默认false
	 */
	function update($table, $values, $where, $updateall=false) {
		if(empty($values)) return -1;
		if(empty($where) && !$updateall) return -1;
		
		$sql = "update t_{$table} set ";
		
		//设置set部分
		$setsql = '';
		foreach($values as $key => $value) {
			$value = $this->addslashes($value);
			if(null==$value || ''==$value){
				$value = "null";
			}
			else $value = "'$value'";
			$setsql .= "c_$key=$value,";
		}
		$cur = time();
		$setsql .= "c_updated=$cur, c_updator='$_SESSION[login]' ";
		$sql .= $setsql;
		
		//设置where部分
		$wheresql = ' where 1=1 ';
		$where = $this->processwhere($where);
		$wheresql .= "and $where";
		
		$sql .= $wheresql;
		return $this->_query($sql);
	}
	
	/**
	 * 删除, 返回行数
	 * 
	 * @param unknown_type $table
	 * @param unknown_type $where
	 * @param unknown_type $deleteall 是否允许删除所有数据,默认为false;仅当条件为空时生效
	 */
	function delete($table, $where, $deleteall=false) {
		$sql = "delete from t_{$table} ";
		
		$wheresql = ' where 1=1 ';
		$where = $this->processwhere($where);
		if(empty($where)){
			if(!$deleteall)
				return;
		}
		else $wheresql .= "and $where";
		$sql .= $wheresql;
		return $this->_query($sql);
	}
	
	function processwhere($where){
		if(empty($where)) return null;
	
		$tmp = preg_replace('/\s*`*([a-zA-Z_][a-zA-Z0-9_]*)`*\s*(=|>|<|!|in|between)/i', ' c_\1 \2', $where);
		//error_log("before:$where\n\tafter:$tmp");
		
		return $tmp;
	}
	
	/**
	 * 简单查询
	 * 
	 * @param unknown_type $table
	 * @param unknown_type $fields 字段数组. $fields只能是一些简单的应用, 如纯字段名, count(*), count(1), distinct等,不能用count(field)
	 * @param unknown_type $where
	 */
	function select($table, $fields, $where=null, $num=null){
		//处理$field
		if(empty($fields)) return null;
		$selfieldstr = "";
		foreach($fields as $f){
			if(strpos($f, "count(")===0) $selfieldstr .= "$f,";
			else $selfieldstr .= "c_$f,";
		}
		//$field = preg_replace('/\s*([a-zA-Z_][a-zA-Z0-9_]*)\s*,/i', ' c_\1,', $field.",");
		$selfieldstr = substr($selfieldstr, 0, -1);
			
		
		$sql = "select $selfieldstr from t_$table ";
		
		$wheresql = ' where 1=1 ';
		$where = $this->processwhere($where);
		if(!empty($where)) 
			$wheresql .= "and $where";
		$sql .= $wheresql;
		
		//echo $sql;
		return $this->query($sql, $num);
	}
	
	/**
	 * 执行sql查询语句，可以是任何语句。当是select时，$num表示取出多少行记录，默认取出所有
	 * @param $sql
	 * @param $num
	 */
	function query($sql, $num = null) {
		$results = array();
		
		$query = $this->_query($sql);
		if(null==$query) $this->halt($sql);

		$i = 1;
		$num = (int)$num;
		while(false != ($row = $this->_fetch_array($query))) {
			$tmp = array();
			foreach($row as $_k => $_v){
				if(substr($_k, 0, 2)=="c_") $_k = substr($_k, 2);
				$tmp[$_k] = $_v;
			}
			$results[] = $tmp;
			if($num>0 && ($i ++ >= $num)) break;
		}
		
		return $results;
	}
	
	/**
	 * 返回一行记录
	 * 
	 * @param unknown_type $table
	 * @param unknown_type $field
	 * @param unknown_type $where
	 */
	function getone1($table, $field, $where){
		$arr = $this->select($table, $field, $where, 1);
		if(isset($arr[0])) {
			return $arr[0];
		} else {
			return false;
		}
	}
	
	/**
	 * 返回一行记录
	 * 
	 * @param unknown_type $table
	 * @param unknown_type $field
	 * @param unknown_type $where
	 */
	function getone($sql){
		$arr = $this->query($sql, 1);
		if(isset($arr[0])) {
			return $arr[0];
		} else {
			return false;
		}
	}
	
	/**
	 * 获取第一个字段的值
	 * 
	 * @param unknown_type $table
	 * @param unknown_type $field
	 * @param unknown_type $where
	 */
	function get1stcolval1($table, $field, $where) {
		$arr = $this->getone1($table, $field, $where);
		if(!empty($arr)) {
			return current($arr);
		} else {
			return '';
		}
	}
	
	/**
	 * 获取第一个字段的值
	 * 
	 * @param unknown_type $sql
	 */
	function get1stcolval($sql) {
		$arr = $this->getone($sql);
		if(!empty($arr)) {
			return current($arr);
		} else {
			return '';
		}
	}

	/**
	 * 获取所有表
	 */
	function getalltables() {
		$tables = array();
		$sql = "SHOW TABLES";
		$query = $this->query($sql);
		$tables = array();
		foreach($query as $row) {
			$tables[] = current($row);
		}
		return $tables;
	}

	/**
	 * 获取$table的create语句。若指定$withdrop=true,那么将会在头部添加DROP语句
	 * @param $table
	 */
	function getcreatetable($table, $withdrop = false) {
		$sql = "SHOW CREATE TABLE `{$table}`";
		$result = current($this->query($sql));
		$sql = $result['Create Table'];
		if($withdrop){
			$sql = "DROP TABLE `{$table}`;\n$sql";
		}
		return $sql;
	}
	
	/**
	 * 获取所有字段
	 * @param $table
	 */
	function getallfields($table) {
		$fields = array();
		$results = $this->query("EXPLAIN $table");
		foreach($results as $result) {
			$fields[] = $result['Field'];
		}
		return $fields;
	}
	
	/**
	 * 出错退出
	 * @param $sql
	 */
	function halt($sql) {
		$this->_error();
		$this->close();
		exit("error in executing :$sql");
	}
	
	function close() {
		$this->_close();
	}
}
?>